﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-19>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：自筹资金节水
//********************************************************************************//
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui
{
    public partial class ZiChouZJJS : BasePage
    {
        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int ZICHOUZJJSID
        {
            get
            {
                return Convert.ToInt32(ViewState["ZICHOUZJJSID"]);
            }
            set
            {
                ViewState["ZICHOUZJJSID"] = value;
            }
        }

        /// <summary>
        /// 标识要添加的用户号是否正确
        /// </summary>
        private bool YONGHUOK
        {
            get
            {
                return Convert.ToBoolean(ViewState["YONGHUOK"]);
            }
            set
            {
                ViewState["YONGHUOK"] = value;
            }
        }

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 自筹资金节水对象
        /// </summary>
        YS_QT_ZICHOUZJJS _YS_QT_ZICHOUZJJS = new YS_QT_ZICHOUZJJS();

        /// <summary>
        /// 自筹资金节水BLL
        /// </summary>
        YS_QT_ZICHOUZJJSBLL _YS_QT_ZICHOUZJJSBLL = new YS_QT_ZICHOUZJJSBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        private string zhandian
        {
            get
            {
                return Convert.ToString(ViewState["zhandian"]);
            }
            set
            {
                ViewState["zhandian"] = value;
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                LoadData(true);
                Display();
            }
            else
            {
                LoadData(false);
            }
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            DataTable dt = _YS_QT_ZICHOUZJJSBLL.SelectYS_QT_ZICHOUZJJsDynamic("1=0", "", null);
            base.Render(sender);
        }

        /// <summary>
        /// grid添加
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //初始化数据
            InitData();
            this.Status = GridViewStatus.Add;
            Display();
        }

        /// <summary>
        /// grid放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            Display();
        }

        /// <summary>
        /// grid修改
        /// </summary>
        protected void lnbModify_Command(object sender, CommandEventArgs e)
        {
            this.ZICHOUZJJSID = Convert.ToInt32(e.CommandArgument);
            this.Status = GridViewStatus.Update;
            Display();
            SetData();
        }

        /// <summary>
        /// grid删除
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            this.ZICHOUZJJSID = Convert.ToInt32(e.CommandArgument);
            _YS_QT_ZICHOUZJJS = _YS_QT_ZICHOUZJJSBLL.SelectYS_QT_ZICHOUZJJ(this.ZICHOUZJJSID, null);
            //执行逻辑删除            
            _YS_QT_ZICHOUZJJS.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            _YS_QT_ZICHOUZJJS.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            _YS_QT_ZICHOUZJJS.I_JLZT = -1;
            _SYS_LOGBLL.Log<YS_QT_ZICHOUZJJS>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "自筹资金节水", OperateState.Delete, _YS_QT_ZICHOUZJJS, null);
            if (_YS_QT_ZICHOUZJJSBLL.InsertOrUpdate(_YS_QT_ZICHOUZJJS))
            {
                               
                PageTools.AlertMsg(this, "操作成功！");
                LoadData(true);
            }
        }

        /// <summary>
        /// 条件查找
        /// </summary>
        protected void conbtnSearch_Click(object sender, EventArgs e)
        {
            LoadData(true);
        }

        /// <summary>
        /// grid保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                SaveData();
            }
        }

        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (this.txtYongHuH.Text == "" || this.txtYongHuH.Text.Length!=7)
            {
                PageTools.AlertMsg(this, "请输入7位用户号！");
                this.txtYongHuH.Focus();
                return false;
            }
            if (this.cmbXiangMuLX.Value == null)
            {
                PageTools.AlertMsg(this, "请选择项目类型！");
                this.cmbXiangMuLX.Focus();
                return false;
            }
            if (this.txtGongChengSL.Text == "0"||this.txtGongChengSL.Text == "" || !PageTools.ValidateString(this.txtGongChengSL.Text,PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this, "请输入正确的项目数量！");
                this.txtGongChengSL.Focus();
                return false;
            }
            if (this.txtRiJieSL.Text == "0"||this.txtRiJieSL.Text == "" || !PageTools.ValidateString(this.txtRiJieSL.Text, PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this, "请输入正确的日节水量！");
                this.txtRiJieSL.Focus();
                return false;
            }
            if (this.txtWANGONGRQ.Text=="")
            {
                PageTools.AlertMsg(this, "请输入竣工日期！");
                this.txtWANGONGRQ.Focus();
                return false;
            }
            int jgrq = -1;
            if (!int.TryParse(this.txtWANGONGRQ.Text, out jgrq))
            {
                PageTools.AlertMsg(this, "请输入正确的竣工日期！");
                this.txtWANGONGRQ.Focus();
                return false;
            }
            else
            { 
                DateTime dt;
                if (!DateTime.TryParse(this.txtWANGONGRQ.Text.Substring(0, 4) + "-" + this.txtWANGONGRQ.Text.Substring(4, 2) + "-01", out dt))
                {
                    PageTools.AlertMsg(this, "请输入正确的竣工日期！");
                    this.txtWANGONGRQ.Focus();
                    return false;
                }
                if (int.Parse(this.txtWANGONGRQ.Text) > Convert.ToInt32(this.concmbY.Value) * 100 + Convert.ToInt32(this.concmbM.Value))
                {
                    PageTools.AlertMsg(this, "竣工日期不能大于统计日期,请输入正确的日期！");
                    this.txtWANGONGRQ.Focus();
                    return false;
                }
            }
            if (this.txtJinE.Text == "0" ||this.txtJinE.Text == "" || !PageTools.ValidateString(this.txtJinE.Text, PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this, "请输入正确的金额！");
                this.txtJinE.Focus();
                return false;
            }
            if (this.YONGHUOK == false)
            {
                LoadYongHuInfo();
            }
            return this.YONGHUOK;
        }

        /// <summary>
        /// 用户号回车事件
        /// </summary>
        protected void btnYonghuhEnter_Click(object sender, EventArgs e)
        {
            LoadYongHuInfo();
        }

        /// <summary>
        /// 项目类型选择变更事件
        /// </summary>
        protected void cmbXiangMuLX_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadYongHuInfo();
            //加载项目单位
            if (this.cmbXiangMuLX.Text == "查漏")
            {
                this.lblDanWei.Text = "次";
            }
            else if (this.cmbXiangMuLX.Text == "冷却塔")
            {
                this.lblDanWei.Text = "台";
            }
            else if (this.cmbXiangMuLX.Text == "器具")
            {
                this.lblDanWei.Text = "件";
            }
            else if (this.cmbXiangMuLX.Text == "工程")
            {
                this.lblDanWei.Text = "项";
            }
        }

        /// <summary>
        /// 站点选中项改变事件
        /// </summary>
        protected void concmbZhanDian_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGuanLiK();
            LoadYWY();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件数据
        /// </summary>
        private void InitControl()
        {
            //加载年
            for (int i = DateTime.Now.Year; i > DateTime.Now.Year - 15; i--)
            {
                this.concmbY.Items.Add(i.ToString());
            }
            this.concmbY.SelectedIndex = 0;
            //根据操作人加载管理部门(站点)
            DataTable dtZhanDian = BusinessTool.GetTextAndValue("YS_JX_ZHANDIAN", "S_ZHANDIANMC", "S_ZHANDIANBH", "-", "S_ZHANDIANBH<>00", "S_ZHANDIANBH asc").Tables[0];
            DataRow drTemp = dtZhanDian.NewRow();
            drTemp[0] = "";
            drTemp[1] = "-1";
            dtZhanDian.Rows.Add(drTemp);
            PageTools.BindComboBox(this.concmbZhanDian, dtZhanDian);
            this.concmbZhanDian.SelectedIndex = 0;
            //加载站点下的管理块
            LoadGuanLiK();
            //加载业务员
            LoadYWY();
            //加载项目类型
            WordsManager manger = new WordsManager();
            DataTable dtXiangMuLX = manger.Instance.FindWordsDataTableByBelongID(WordsStruct.BelongID.自筹资金节水项目类型);
            PageTools.BindComboBox(this.cmbXiangMuLX, dtXiangMuLX);
            drTemp = dtXiangMuLX.NewRow();
            drTemp[0] = "";
            drTemp[1] = 0;
            dtXiangMuLX.Rows.InsertAt(drTemp, 0);
            PageTools.BindComboBox(this.concmbXiangMuLX, dtXiangMuLX);
            this.concmbXiangMuLX.SelectedIndex = 0;
            //获取当前水量年月
            int ym = Convert.ToInt32(_spHelper.P_YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString()).Tables[0].Rows[0][0]);
            this.concmbY.Value = ym / 100;
            this.concmbM.Value = ym % 100;
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtJinE.Text = "";
            this.txtWANGONGRQ.Text = "";
            this.txtMiaoShu.Text = "";
            this.txtRiJieSL.Text = "";
            this.txtGongChengSL.Text = "";
            this.txtYongHuH.Text = "";
            this.txtYongHuM.Text = "";
            this.txtZhanDian.Text = "";
            this.cmbXiangMuLX.Value = "";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData(bool reGet)
        {
            if (reGet)
            {
                string condition = "I_JLZT=0 AND "
                    + (this.concmbZhanDian.Value.ToString() == "-1" ? "1=1" : "S_ZHANDIANBH='" + this.concmbZhanDian.Value.ToString() + "'")
                    + " AND I_Y=" + Convert.ToInt32(this.concmbY.Value)
                    + " AND I_M=" + Convert.ToInt32(this.concmbM.Value)
                    + (this.concmbGuanLiK.SelectedIndex == 0 ? "" : " AND S_GUANLIKH='" + this.concmbGuanLiK.Value.ToString() + "'")
                    + (this.concmbYWY.SelectedIndex == 0 ? "" : " AND I_YWY=" + this.concmbYWY.Value.ToString())
                    + (this.contxtYongHuH.Text == "" ? "" : " AND S_YONGHUH like '%" + this.contxtYongHuH.Text + "%'")
                    + (this.contxtYongHuM.Text == "" ? "" : " AND S_YONGHUM like '%" + this.contxtYongHuM.Text + "%'")
                    + (this.concmbXiangMuLX.SelectedIndex == 0 ? "" : " AND I_XIANGMULX=" + Convert.ToInt32(this.concmbXiangMuLX.Value))
                    + (this.contxtRiJieSL.Text == "" ? "" : " AND i_rijiesl" + this.concmbRiJieSLValPrePare.Value.ToString() + "'" + this.contxtRiJieSL.Text + "'")
                    ;
                this.dtSource = _YS_QT_ZICHOUZJJSBLL.SelectYS_QT_ZICHOUZJJsDynamic(condition, null);
            }
            gvList1.DataSource = this.dtSource;
            gvList1.DataBind();
            if (reGet)
            {
                //获取统计信息
                DataTable dtTJ = _spHelper.P_YS_ZICHOUZJJSTJ(Convert.ToInt32(this.concmbY.Value), Convert.ToInt32(this.concmbM.Value), this.concmbZhanDian.Value.ToString() == "" ? null : this.concmbZhanDian.Value.ToString(), this.concmbGuanLiK.SelectedIndex == 0 ? null : this.concmbGuanLiK.Value.ToString(), this.concmbYWY.SelectedIndex == 0 ? null : (int?)Convert.ToInt32(this.concmbYWY.Value)).Tables[0];
                this.lblRecordCount.Text = "共有" + dtTJ.Rows[0]["RECORDCOUNT"].ToString() + "个项目";
                this.lblGongChengSLSUM.Text = "数量：" + dtTJ.Rows[0]["SLSUM"].ToString();
                this.lblJinESUM.Text = "投入资金：" + dtTJ.Rows[0]["JINESUM"].ToString();
                this.lblRiJieSLSUM.Text = "日节水量：" + dtTJ.Rows[0]["JSLSUM"].ToString();
            }
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    btnAdd.Visible = true;
                    GridViewContentTable.Visible = false;
                    this.gvList1.Enabled = true;
                    break;
                case GridViewStatus.Search:
                    btnAdd.Visible = true;
                    GridViewContentTable.Visible = true;
                    break;
                case GridViewStatus.Update:
                    btnAdd.Visible = false;
                    GridViewContentTable.Visible = true;
                    this.gvList1.Enabled = false;
                    this.lblOperateTip.Text = "修改";
                    break;
                case GridViewStatus.Add:
                    btnAdd.Visible = false;
                    GridViewContentTable.Visible = true;
                    this.gvList1.Enabled = false;
                    this.lblOperateTip.Text = "添加";
                    this.txtYongHuH.Focus();
                    break;
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //添加
            if (this.Status == GridViewStatus.Add)
            {
                _YS_QT_ZICHOUZJJS.ID = 0;
                _YS_QT_ZICHOUZJJS.I_JLZT = 0;
                operateState = OperateState.Add;
            }
            //修改
            else
            {
                _YS_QT_ZICHOUZJJS = _YS_QT_ZICHOUZJJSBLL.SelectYS_QT_ZICHOUZJJ(this.ZICHOUZJJSID, null);
                operateState = OperateState.Modify;
            }
            this._YS_QT_ZICHOUZJJS.I_Y = Convert.ToInt32(this.concmbY.Value);
            this._YS_QT_ZICHOUZJJS.I_M = Convert.ToInt32(this.concmbM.Value);
            this._YS_QT_ZICHOUZJJS.S_TONGJIYM = (this._YS_QT_ZICHOUZJJS.I_Y * 100 + this._YS_QT_ZICHOUZJJS.I_M).ToString();
            this._YS_QT_ZICHOUZJJS.N_JINE = decimal.Parse(this.txtJinE.Text);
            this._YS_QT_ZICHOUZJJS.S_WANGONGRQ=this.txtWANGONGRQ.Text;
            this._YS_QT_ZICHOUZJJS.S_MIAOSHU = this.txtMiaoShu.Text;
            this._YS_QT_ZICHOUZJJS.I_RIJIESL = int.Parse(this.txtRiJieSL.Text);
            this._YS_QT_ZICHOUZJJS.I_GONGCHENGSL = int.Parse(this.txtGongChengSL.Text);
            this._YS_QT_ZICHOUZJJS.S_YONGHUH = this.txtYongHuH.Text;
            this._YS_QT_ZICHOUZJJS.S_YONGHUM = this.txtYongHuM.Text;
            this._YS_QT_ZICHOUZJJS.S_SHURUBM = this.txtZhanDian.Text;
            //this._YS_QT_ZICHOUZJJS.S_ZHANDIANBH = this.hdCollection.Get("S_ZHANDIANBH").ToString();
            this._YS_QT_ZICHOUZJJS.S_ZHANDIANBH = this.zhandian;
            this._YS_QT_ZICHOUZJJS.I_XIANGMULX = Convert.ToInt32(this.cmbXiangMuLX.Value);
            this._YS_QT_ZICHOUZJJS.S_XIANGMULX = this.cmbXiangMuLX.Text;
            this._YS_QT_ZICHOUZJJS.S_DANWEI = this.lblDanWei.Text;
            this._YS_QT_ZICHOUZJJS.S_XIUGAIR = LoginInfo.CurrentUser.Account;
            this._YS_QT_ZICHOUZJJS.D_XIUGAIRQ = BusinessTool.GetDataBaseDateTime();
            this._YS_QT_ZICHOUZJJS.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            this._YS_QT_ZICHOUZJJS.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            _SYS_LOGBLL.Log<YS_QT_ZICHOUZJJS>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "自筹资金节水", operateState, _YS_QT_ZICHOUZJJS, null);
            if (_YS_QT_ZICHOUZJJSBLL.InsertOrUpdate(_YS_QT_ZICHOUZJJS, null))
            {
                
                PageTools.AlertMsg(this, "保存成功！");
                this.Status = GridViewStatus.Init;
                Display();
                LoadData(true);
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
        }

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            _YS_QT_ZICHOUZJJS = _YS_QT_ZICHOUZJJSBLL.SelectYS_QT_ZICHOUZJJ(this.ZICHOUZJJSID, null);
            this.txtJinE.Text = this._YS_QT_ZICHOUZJJS.N_JINE.ToString();
            this.txtWANGONGRQ.Text = this._YS_QT_ZICHOUZJJS.S_WANGONGRQ;
            this.txtMiaoShu.Text = this._YS_QT_ZICHOUZJJS.S_MIAOSHU;
            this.txtRiJieSL.Text = this._YS_QT_ZICHOUZJJS.I_RIJIESL.ToString();
            this.txtGongChengSL.Text = this._YS_QT_ZICHOUZJJS.I_GONGCHENGSL.ToString();
            this.txtYongHuH.Text = this._YS_QT_ZICHOUZJJS.S_YONGHUH;
            this.txtYongHuM.Text = this._YS_QT_ZICHOUZJJS.S_YONGHUM;
            this.txtZhanDian.Text = this._YS_QT_ZICHOUZJJS.S_SHURUBM;
            this.cmbXiangMuLX.Value = this._YS_QT_ZICHOUZJJS.I_XIANGMULX.ToString();
        }

        /// <summary>
        /// 查询用户信息
        /// </summary>
        private void LoadYongHuInfo()
        {
            YS_YG_YONGHUXXBLL bllTemp = new YS_YG_YONGHUXXBLL();
            //查询用户信息
            DataRowCollection drc = bllTemp.SelectYS_YG_YONGHUXXesDynamic("I_JLZT=0 AND I_LEIXING=0 AND S_YONGHUH='" + this.txtYongHuH.Text + "'", null).Rows;
            if (drc.Count == 0)
            {
                PageTools.AlertMsg(this, "该用户不是系统中的考核用户,请您重新输入用户号。");
                this.YONGHUOK = false;
                return;
            }
            this.txtYongHuM.Text = drc[0]["S_YONGHUM"].ToString();
            //获取站点
            YS_JX_ZHANDIANBLL bllTemp2 = new YS_JX_ZHANDIANBLL();
            DataRowCollection drc2 = bllTemp2.SelectYS_JX_ZHANDIANsDynamic("I_JLZT=0 AND  S_ZHANDIANBH='" + drc[0]["S_ZHANDIANBH"] + "'", null).Rows;
            this.txtZhanDian.Text = drc2[0]["S_ZHANDIANMC"].ToString();
            //this.hdCollection.Set("S_ZHANDIANBH", drc[0]["S_ZHANDIANBH"]);
            this.zhandian = Convert.ToString(drc[0]["S_ZHANDIANBH"]);
            this.YONGHUOK = true;
        }

        /// <summary>
        /// 根据站点加载管理块
        /// </summary>
        private void LoadGuanLiK()
        {
            //加载站点下的管理块
            DataTable dtGLK = BusinessTool.GetTextAndValue(
                    "YS_JX_GUANLIK", "S_GUANLIKMC", "S_GUANLIKH", null, string.Format("s_zhandian='{0}'", this.concmbZhanDian.Value)
                ).Tables[0];
            DataRow drTemp = dtGLK.NewRow();
            drTemp[0] = "";
            drTemp[1] = "";
            dtGLK.Rows.InsertAt(drTemp, 0);
            PageTools.BindComboBox(
                this.concmbGuanLiK, dtGLK
            );
            if (this.concmbGuanLiK.Items.Count != 0)
            {
                this.concmbGuanLiK.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 根据站点加载业务员
        /// </summary>
        private void LoadYWY()
        {
            PageTools.LoadCombox(this.concmbYWY, PageTools.eLoadType.业务员, BusinessTool.ZHANDIANBH(), null, "");
            //DataTable dtYWY = BusinessTool.GetTextAndValue(
            //         "View_AS_UserInfo", "USERNAME", "USERID", null, string.Format("ZHANDIANBH='{0}' and ROLENAME like '%业务员%'", this.concmbZhanDian.Value)
            //     ).Tables[0];
            //DataRow drTemp = dtYWY.NewRow();
            //drTemp[0] = "";
            //drTemp[1] = 0;
            //dtYWY.Rows.InsertAt(drTemp, 0);
            //PageTools.BindComboBox(
            //    this.concmbYWY, dtYWY
            //);
            //if (this.concmbYWY.Items.Count != 0)
            //{
            //    this.concmbYWY.SelectedIndex = 0;
            //}
        }

        /// <summary>
        /// 获取项目类型
        /// </summary>
        /// <returns></returns>
        public string GetXMLX(int lx)
        {
            return lx == 1 ? "查陋" : lx == 2 ? "冷却塔" : lx == 3 ? "器具" : lx == 4 ? "工程" : "其他";
        }

        #endregion                
    }
}